﻿package com.mixmedia.view {
	import flash.display.SimpleButton;
	import flash.events.EventDispatcher;
	
	import com.mixmedia.navigation.Navigation;
	import com.mixmedia.navigation.NavigationEvent;
	import com.mixmedia.view.ButtonEvt;
	import com.mixmedia.view.events.ButtonClipEvent;	

	/**
	 * @author Colin
	 * decorator of ButtonClip
	 */
	final public class NavButton extends EventDispatcher implements IButtonClip, IFASTEventDispatcher{
		private var base:ButtonEvt;
		private var nav:Navigation;
	
		public var navKey:String;
		public var targetContainer: String;
		private var elements : Array = [];

		public function NavButton(hitarea:SimpleButton,navKey:String,targetContainer:String){
			base = new ButtonEvt(hitarea);
			base.when(ButtonClipEvent.CLICK, this,click);
	
			this.navKey = navKey;
			this.targetContainer = targetContainer;
	
			nav = Navigation.instance();
			nav.when(NavigationEvent.CHANGE,this,highlightButton);
		}
	
		private function click(e:ButtonClipEvent):void{
			var nav:Navigation = Navigation.instance();
			nav.clearNavStackRequests();
			nav.changeSection(navKey, targetContainer, false, base.getBase());
		}

		public function highlightButton(e:NavigationEvent):void{
			//if me in tree, highlight, else, dim
			var id:String = e.navKey;//1, 1_2_3, 5, 3;
			for(var i:int=0;i<id.split("_").length;i++){
				if(this.navKey==id.split("_",i+1).join("_")){
					base.isHighlight = true;
					return;
				}
			}
			base.isHighlight = false;
		}
		
		public function addElement(element : IButtonElement) : void {
			elements.push(element);
			base.addElement(element);
		}
		
		public function getElements():Array{
			return elements;
		}
		
		public function get isHighlight():Boolean{
			return base.isHighlight;
		}
		
		public function select() : void {
			base.select();
		}
		
		public function setMouseOverDelay(num : Number) : void {
			base.setMouseOverDelay(num);
		}
		
		public function setMouseOutDelay(num : Number) : void {
			base.setMouseOutDelay(num);
		}
		
		public function clearMouseOver() : void {
			base.clearMouseOver();
		}
		
		public function clearMouseOut() : void {
			base.clearMouseOut();
		}
		
		public function when(eventType : String, whichObject : Object, callFunction : Function) : void {
			base.when(eventType,whichObject,callFunction);
		}
	}
}